{layout name="layout1" /}
<style>
    .layui-table-cell{
        height: 60px;
        max-width: 100%;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
                <div class="layui-colla-item">
                    <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
                    <div class="layui-colla-content layui-show">
                        <p>*发布商品时可以选择供货商，方便货源管理。</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="white-space: nowrap;">商户名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keyword" id="keyword" placeholder="" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-unit {$view_theme_color}" lay-submit
                            lay-filter="supplier-search">查询
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-primary layuiadmin-btn-unit }" lay-submit
                            lay-filter="supplier-clear-search">清空查询
                    </button>
                </div>

            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-sm layuiadmin-btn-unit {$view_theme_color}" data-type="add">新增供货商
                </button>
                <!--                <button class="layui-btn layui-btn-sm layuiadmin-btn-unit layui-btn-danger" data-type="batchdel">删除所选</button>-->
            </div>

            <table id="supplier_lists" lay-filter="supplier_lists"></table>

            <script type="text/html" id="supplier-operation">
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="toexamine" vdata="1">审核</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm operation-btn" lay-event="remarks">驳回</a>
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="toexamine" vdata="3">禁用</a>


            </script>
        </div>
    </div>
</div>


<script>
    layui.config({
        version: "{$front_version}",
        base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'like', 'table', 'form'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , like = layui.like;

        //监听搜索
        form.on('submit(supplier-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('supplier_lists', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                },
            });
        });
        //清空查询
        form.on('submit(supplier-clear-search)', function () {
            $('#keyword').val('');//清空输入框
            //刷新列表
            table.reload('supplier_lists', {
                where: []
            });
        });

        //事件
        var active = {
            add: function () {
                {
                    layer.open({
                        type: 2
                        , title: '新增供货商'
                        , content: '{:url("Supplier/add")}'
                        , area: ['90%', '90%']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'supplier-submit'
                                , submit = layero.find('iframe').contents().find("#supplier-submit");
                            //监听提交


                            iframeWindow.layui.form.on('submit(supplier-submit)', function (data) {
                                var field = data.field;
                                console.log(field);

                                {
                                    like.ajax({
                                        url: '{:url("Supplier/add")}',
                                        data: field,
                                        type: "post",
                                        success: function (res) {
                                            if (res.code == 1) {
                                                layui.layer.msg(res.msg, {
                                                    offset: '15px'
                                                    , icon: 1
                                                    , time: 1000
                                                });
                                                layer.close(index); //关闭弹层
                                                table.reload('supplier_lists'); //数据刷新
                                            } else {
                                                layer.msg(res.msg, {
                                                    offset: '15px'
                                                    , icon: 2
                                                    , time: 1000
                                                });
                                            }

                                        }
                                    });
                                }
                            });
                            submit.trigger('click');
                        },

                    })
                }
            }

            , batchdel: function () { //删除所选
                var checkStatus = table.checkStatus('supplier_lists')
                    , checkData = checkStatus.data; //得到选中的数据
                //是否已选数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                } else {
                    //获取所选id
                    ids = [];
                    for (var i in checkData) {
                        ids.push(checkData[i]['id']);
                    }

                    layer.confirm('确定删除所选供货商信息？', function (index) {
                        like.ajax({
                            url: '{:url("Supplier/del")}',
                            data: {id: ids},
                            type: "post",
                            success: function (res) {
                                if (res.code == 1) {
                                    layui.layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    });
                                    layer.close(index); //关闭弹层
                                    table.reload('supplier_lists'); //数据刷新
                                } else {
                                    layer.msg(res.msg, {
                                        offset: '15px'
                                        , icon: 2
                                        , time: 1000
                                    });
                                }
                            }
                        });
                        layer.close(index);
                    });
                }
            }
        };
        $('.layui-btn.layuiadmin-btn-unit').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.render({
            id: 'supplier_lists',
            elem: '#supplier_lists'
            , url: '{:url("supplier/lists")}'
            , cols: [[
                {field: 'id', title: '序号'}
                , {field: 'store_full_name', width: 150, title: '企业全称'}
                , {field: 'store_name', width: 150, title: '店铺名称'}
                , {
                    field: 'store_url', width: 150, title: '店铺链接', templet: function (value, row, index) {
                        var i = "";
                        if (value.store_url) {
                            i = "<span class='supplier-link layui-btn layui-btn-xs layui-btn-normal'  data-link='" + value.store_url + "'>" + value.store_url + "</span>"
                        }
                        return i
                    }
                }
                , {field: 'wx_code', width: 250, title: '微信号'}
                , {field: 'tel_number', width: 250, title: '手机号'}
                , {
                    field: 'license', width: 250, title: '执照链接', templet: function (value, row, index) {
                        var i = "";
                        if (value.license) {
                            i = "<img class='image-show' src='" + value.license + "'>"
                        }
                        return i
                    }
                }
                , {
                    field: 'status', width: 250, title: '商户状态', templet: function (value, row, index) {
                        var i = "";
                        if (value.status == '0') {
                            i = '<span class=\"layui-badge layui-bg-blue\">待审核</span>';
                        } else if (value.status == '1') {
                            i = '<span class=\"layui-badge layui-bg-green\">正常</span>';
                        } else if (value.status == '2') {
                            i = '<span class=\"layui-badge layui-bg-orange\">审核被拒</span>';
                        } else if (value.status == '3') {
                            i = '<span class=\"layui-badge layui-bg-red\">禁用</span>';
                        }
                        return i;
                    }
                }
                , {fixed: 'right', title: '操作', width: 400, align: 'center', toolbar: '#supplier-operation'}
            ]]
            , page: true
            , text: {none: '暂无数据！'}
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count, //解析数据长度
                    "data": res.data.lists, //解析数据列表
                };
            }
            , done: function (res, curr, count) {
                // 解决操作栏因为内容过多换行问题
                $(".layui-table-main tr").each(function (index, val) {
                    $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                    $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                });
            }
        });

        $(document).on("click", ".supplier-link", function () {
            var url = $(this).attr("data-link");
            window.open(url);
        });

        //图片放大
        $(document).on('click', '.image-show', function () {
            var src = $(this).attr('src');
            like.showImg(src, 600);
        });

        table.on('tool(supplier_lists)', function (obj) {
            if (obj.event === 'del') {
                var id = obj.data.id;
                var name = obj.data.store_full_name;
                layer.confirm('确定删除供货商:<span style="color: red">' + name + "</span>", function (index) {
                    like.ajax({
                        url: '{:url("Supplier/del")}',
                        data: {id: id},
                        type: "post",
                        success: function (res) {
                            if (res.code == 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                table.reload('supplier_lists'); //数据刷新
                                obj.del();
                            } else {
                                layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '修改供货商信息'
                    , content: '{:url("Supplier/edit")}?id=' + id
                    , area: ['60%', '60%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#supplier-submit-edit');
                        console.log(data);
                        //监听提交
                        iframeWindow.layui.form.on('submit(supplier-submit-edit)', function (data) {
                            var field = data.field;
                            like.ajax({
                                url: '{:url("Supplier/edit")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        console.log(1);
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('supplier_lists'); //数据刷新
                                    }
                                    else {
                                        console.log(2);
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }

                            });
                        });
                        submit.trigger('click');
                    }
                })
            } else if (obj.event === 'edit') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '修改供货商信息'
                    , content: '{:url("Supplier/edit")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#supplier-submit-edit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(supplier-submit-edit)', function (data) {
                            var field = data.field;
                            like.ajax({
                                url: '{:url("Supplier/edit")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    console.log(res);
                                    if (res.code == 1) {
                                        console.log(1);
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('supplier_lists'); //数据刷新
                                    }
                                    else {
                                        console.log(2);
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }

                            });
                        });
                        submit.trigger('click');
                    }
                })
            } else if (obj.event === 'toexamine') {
                var id = obj.data.id;
                var name = obj.data.store_full_name;
                var sta = this.getAttribute('vdata');
                var i = '';
                if (sta == "1") {
                    i = '确定审核通过供货商:';
                } else if (sta == '2') {
                    i = '确定驳回供货商:';
                } else if (sta == '3') {
                    i = '确定禁用供货商:';
                }

                layer.confirm(i + '<span style="color: red">' + name + "</span>", function (index) {
                    like.ajax({
                        url: '{:url("Supplier/edit")}',
                        data: {id: id, status: sta},
                        type: "post",
                        success: function (res) {
                            if (res.code == 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                table.reload('supplier_lists'); //数据刷新
                                // obj.del();
                            } else {
                                layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                            }
                        }
                    });
                    layer.close(index);
                });
            }


            if (obj.event === 'remarks') {
                var id = obj.data.id;
                layer.prompt({
                    title: '备注信息',
                    formType: 2,
                    value: ''
                }, function (value, index) {
                    layer.close(index);
                    like.ajax({
                        url: '{:url("supplier/remarks")}'
                        , data: {'id': id, "supplier_remarks": value}
                        , type: 'post'
                        , success: function (res) {
                            if (res.code === 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                            }
                        }
                    });

                });

            }
        });
    });
</script>